package com.mao.dao.product;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Component;

import com.mao.dao.BaseDAOHibernate;
import com.mao.model.product.Product;
import com.mao.model.product.ProductClass;

@Component("productClassDao")
public class ProductClassDao extends BaseDAOHibernate {
	public List<ProductClass> getAllProductClass(Long storeid) {
		Session session = this.getSession();
		String hql = "from ProductClass t where t.parent.id is null and t.status = "
				+ ProductClass.STATUS_NORMAL + " and t.store.id=" + storeid;
		Query query = session.createQuery(hql);
		List<ProductClass> list = query.list();
		ProductClass pcRes = null;
		if(list != null && list.size() > 0){
			for(int i=0; i<list.size(); i++){
				pcRes = list.get(i);
				if (!Hibernate.isInitialized(pcRes.getParent())) {
					if(pcRes.getParent().getId() != 0)
						Hibernate.initialize(pcRes.getParent());
				}
			}
			
		}
		
		return query.list();
	}
	
	public List<ProductClass> getAllProductClass() {
		Session session = this.getSession();
		String hql = "from ProductClass t";
		Query query = session.createQuery(hql);
		return query.list();
	}

	public ProductClass saveProductClass(ProductClass pc) {
		getHibernateTemplate().save(pc);
		return pc;
	}

	public ProductClass updateProductClass(ProductClass pc) {
		getHibernateTemplate().update(pc);
		return pc;
	}

	public ProductClass getProductClassById(long id) {
		Session session = this.getSession();
		String hql = "From ProductClass pc where pc.id=:id";
		Query query = session.createQuery(hql);
//		query.setParameter("status", ProductClass.STATUS_NORMAL);
		query.setParameter("id", id);
		List<ProductClass> list = query.list();
		ProductClass pcRes = null;
		if (list != null) {
			pcRes = list.get(0);
			if (!Hibernate.isInitialized(pcRes.getLowClass())) {
				Hibernate.initialize(pcRes.getLowClass());
			}
			if (!Hibernate.isInitialized(pcRes.getParent())) {
				if(pcRes.getParent().getId() != 0)
					Hibernate.initialize(pcRes.getParent());
			}
			return pcRes;
		}
		return null;
	}

	public String valDelProClass(long id) {
		// 返回1表示该分类下已有分类 2表示该分类下已有商品信息
		Session session = this.getSession();
		String hql = "From ProductClass pc where pc.status=:status and pc.id=:id";
		Query query = session.createQuery(hql);
		query.setParameter("status", ProductClass.STATUS_NORMAL);
		query.setParameter("id", id);
		List<ProductClass> list = query.list();
		ProductClass pcRes = null;
		if (list != null) {
			pcRes = list.get(0);
			if (!Hibernate.isInitialized(pcRes.getLowClass())) {
				Hibernate.initialize(pcRes.getLowClass());
			}
			if (pcRes.getLowClass().size() > 0) {
				return "1";
			}
		}
		Session psession = this.getSession();
		String phql = "From Product p where p.status=:status and p.proClass.id=:id";
		Query pquery = psession.createQuery(phql);
		pquery.setParameter("status", ProductClass.STATUS_NORMAL);
		pquery.setParameter("id", id);
		List<Product> plist = pquery.list();
		if(plist.size() > 0){
			return "2";
		}
		return "0";
	}
}
